package cz.fit.vutbr.pis2012.augeaswh.control.ekonom.discounts;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Column;

import cz.fit.vutbr.pis2012.augeaswh.managers.DiscountsManager;
import cz.fit.vutbr.pis2012.augeaswh.managers.StockTypesManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.Discount;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockType;


@ManagedBean
@SessionScoped
public class DiscountsEditorBean implements Serializable
{
	
	@EJB
	private DiscountsManager dm;
	@EJB 
	private StockTypesManager stm;
	
	private static final long serialVersionUID = -3939062955769937958L;
	public String title = "Discount edit";
	private Discount discount;
	private StockType stockType;


	public StockType getStockType() {
		return stockType;
	}

	public void setStockType(StockType stockType) {
		this.stockType = stockType;
	}

	@Column(name="discount_type")
	private String discountType;
    private BigDecimal discountValue;
	private BigDecimal time;
	
	
	public void setDiscount(Discount discount) {
		this.discount = discount;
		discountType = null;
		stockType = null;
		time = null;
		discountValue = null;
		stockType = stm.getStockTypes().get(0);
		
		if(this.discount!=null) {
			this.discountValue = discount.getDiscount();
			this.discountType = discount.getDiscountType();
			this.time = discount.getTime();
			this.stockType = discount.getStockType();
		}
		
	    
	}
	
public String submit() {
		
		if(discount==null) {
			this.discount = new Discount();
		}
		
		this.discount.setTime(time);
		this.discount.setDiscountType(discountType);
		this.discount.setStockType(stockType);
		this.discount.setDiscount(discountValue);
		dm.saveOrUpdateDiscount(discount);
		setDiscount(null);
		return "back";
	}

	public BigDecimal getDiscountValue() {
		return discountValue;
	}

	public void setDiscountValue(BigDecimal discountValue) {
		this.discountValue = discountValue;
	}

	public String getDiscountType() {
		return discountType;
	}

	public void setDiscountType(String discountType) {
		this.discountType = discountType;
	}

	public BigDecimal getTime() {
		return time;
	}

	public void setTime(BigDecimal time) {
		this.time = time;
	}


	public Discount getDiscount() {
		return discount;
	}
	
	public String getTitle() {
		if(this.discount == null) {
			return "New discount";
		}
		else {
			return "Edited discount";
		}			
	}


}
